<?php namespace lfy\template;

\lfy\setconst('LFY_LEAF_PROPERTY_LANG', 'en-gb');
\lfy\setconst('LFY_LEAF_PROPERTY_DOCTYPE', 'html5');

//! an html markup template

class html extends \lfy\template{

	//! header links array, in the form array( array('attribute'=>'value') )

	public static $links = array();

	//! header scripts array, as $links

	public static $scripts = array();

	//! meta names to search for in leaf properties

	public static $meta_names = array(
		'tags', 'description'
	);

	//! doctype declarations

	public static $declarations = array(
		'html4-strict' => "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \n\t\"http://www.w3.org/TR/html4/strict.dtd\">",
		'html4-transitional' => "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \n\t\"http://www.w3.org/TR/html4/loose.dtd\">",
		'html5' => '<!DOCTYPE HTML>',
		'xhtml1-strict' => "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \n\t\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">",
		'xhtml1-transitional'=>"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \n\t\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">",
		'xhtml1.1' => "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"  \n\t\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">"					  
	);

	//! determine the declaration desired by leaf

	protected static function get_declaration($leaf){
		$top_class = $leaf->template;
		$t = $leaf->property('doctype');
		$declarations = $top_class::leaf_merge('declarations', $leaf);
		if(!isset($declarations[$t])) \lfy\back_error("invalid doctype: '$t'", E_USER_ERROR);
		return $declarations[$t];
	}

	//! open function. Adds declaration, opens html tag and builds html head element (omits body open tag)

	protected static function open($leaf){

		$top_class = $leaf->template;

		// declaration
		\lfy\putn(self::get_declaration($leaf));	

		\lfy\putn('<html lang="'.$leaf->property('lang').'">');
	
		// head and title
		\lfy\putn('<head>');
		\lfy\putn("<title>".$leaf->property('html_title', 'title')."</title>");
	
		// meta
		foreach(headers_list() as $h){
			list($k, $v) = explode(':', $h);
			\lfy\putn("<meta http-equiv=\"$k\" content=\"".trim($v)."\" />");
		}

		foreach($top_class::leaf_merge('meta_names', $leaf) as $name){
			if($content = $leaf->property($name)) \lfy\putn("<meta name=\"$name\" content=\"$content\" />");
		}

		foreach($top_class::leaf_merge('links', $leaf) as $link){
			if($link && !is_scalar($link)){
				$s = "<link ";
				foreach($link as $k=>$v) $s .= "$k=\"$v\" ";
				$s .= ' />';
				\lfy\putn($s);
			}else trigger_error("Leaf template link is type '".gettype($link)."' but should be passed as array('attribute'=>'value')", E_USER_WARNING);
		}

		foreach($top_class::leaf_merge('scripts', $leaf) as $script){
			if($script && !is_scalar($script)){
				$s = "<script";
				foreach($script as $k=>$v) $s .= " $k=\"$v\"";
				$s .= '></script>';
				\lfy\putn($s);
			}else trigger_error("Leaf template script is type '".gettype($script)."' but should be passed as array('attribute'=>'value')", E_USER_WARNING);
		}

		if($insert = $leaf->property('head_insert')){
			\lfy\putn($insert);
		}
		// close head
		\lfy\putn("</head>\n");
	}

	//! closes html tag

	protected static function close($leaf){
		\lfy\putn('</html>');
	}

}

